//
//  LZManagerStaffPresenter.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/9.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZManagerStaffPresenter.h"
#import "NetworkServiceManager.h"
#import "MJExtension.h"
#import "NSString+Additions.h"
#import "LZUserModel.h"

#import "LZStaffModel.h"

extern NSString *merchant;
static NSString *const kStaffAddURL = @"/staff/add"; //添加员工
static NSString *const kStaffListURL = @"/staff/list"; //员工列表
static NSString *const kStaffDeleteURL = @"/staff/delete"; //认证接口
static NSString *const kStaffEditURl = @"/staff/update"; //编辑员工
@implementation LZManagerStaffPresenter

#pragma mark -
#pragma mark 添加新员工
/**
 添加新员工
 
 @param staffModel <#staffModel description#>
 @param successBlock <#successBlock description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)addNewStaffWithModel:(LZStaffModel *)staffModel Success:(PresenterCommonSuccess)successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kStaffAddURL);
    NSMutableDictionary *dic = staffModel.mj_keyValues;
    [dic setObject:[LZUserModel Instance].userToken forKey:@"token"];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        if ([LZBasePresenter isRequestStatusCode:resultData fail:failureBlock]) {
            if (successBlock) {
                successBlock(nil);
            }
        }else{
            NSInteger code = [resultData[@"statusCode"] integerValue];
            if (code == kCodeUseNameRegistered) {
                if (failureBlock) {
                    failureBlock(@"用户名已经使用,没有翻译");
                }
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];
}


#pragma mark -
#pragma mark 员工列表
- (void)getStaffListWithTimeStamp:(NSTimeInterval)timeStamp PageNum:(NSInteger)page Limit:(NSInteger)limit Success:(void(^)(NSArray *listArr, NSInteger totalCount))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant, kStaffListURL);
    NSMutableDictionary *dic= @{}.mutableCopy;
    [dic setObject:[LZUserModel Instance].sellerId forKey:@"sellerId"];
    [dic setObject:[NSNumber numberWithInteger:page] forKey:@"cpage"];
    [dic setObject:[NSNumber numberWithInteger:limit] forKey:@"limit"];
    [dic setObject:[NSNumber numberWithDouble:timeStamp] forKey:@"timestamp"];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCode:resultData fail:failureBlock]) {
            NSMutableArray *resultArr = @[].mutableCopy;
            NSArray *arr = resultData[@"data"][@"list"];
            NSInteger totalCountTemp = [resultData[@"data"][@"totalCount"] integerValue];
            for (int i = 0; i < arr.count; i ++) {
                LZStaffModel *m = [LZStaffModel mj_objectWithKeyValues:arr[i]];
                [resultArr addObject:m];
            }
            
            if (successBlock) {
                successBlock(resultArr.copy,totalCountTemp);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];
}


#pragma mark -
#pragma mark 删除员工
- (void)deleteStaffWithStaffID:(NSString *)staffId Success:(PresenterCommonSuccess)successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kStaffDeleteURL);
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setObject:staffId forKey:@"id"];
    [dic setObject:[LZUserModel Instance].userToken forKey:@"token"];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCode:resultData fail:failureBlock]) {
            if (successBlock) {
                successBlock(nil);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];
}

#pragma mark -
#pragma mark 编辑员工
- (void)editStaffWithModel:(LZStaffModel *)staffModel Success:(PresenterCommonSuccess)successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kStaffEditURl);
    NSMutableDictionary *dic = staffModel.mj_keyValues;
    [dic setObject:[LZUserModel Instance].userToken forKey:@"token"];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCode:resultData fail:failureBlock]) {
            if (successBlock) {
                successBlock(nil);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];
    
}

@end
